package com.webwork.mood.system.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.google.gson.Gson;
import com.webwork.mood.common.controller.SuperCtrl;
import com.webwork.mood.system.dataobject.Menu;
import com.webwork.mood.system.dataobject.Role;
import com.webwork.mood.system.dataobject.User;
import com.webwork.mood.system.service.MenuService;

@Controller
@RequestMapping(value = "/main")
public class MainController extends SuperCtrl {

	@Autowired
	private MenuService menuSve;
	
	@RequestMapping(value = "getMenuUrl")
	@ResponseBody
	public ResponseEntity<String> getMenuUrl(HttpServletRequest request) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		User user = (User) request.getSession(true).getAttribute("user");
		System.out.println(user.getUserId());
		Role role = Role.NOSET;
		if (user.getUserId().equalsIgnoreCase("admin")) {
			role = Role.ADMIN;
		} else if (user.getIsManager() == 1) {
			role = Role.MANAGER;
		} else {
			role = Role.EMPLOYEE;
		}
		List<Menu> list = menuSve.getMenuByRole(role);
		return new ResponseEntity<String>(new Gson().toJson(list).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value = "/welcome")
	@Override
	public String mainPage() {
		return "welcome";
	}
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
	
	@RequestMapping(value="uploadFile")
	@ResponseBody
	public ResponseEntity<String> upLoadTxtFile(MultipartHttpServletRequest multipartRequest) {
		String fileToUploadId = multipartRequest.getParameter("fileToUploadId");//前台上传控件ID
//		CommonsMultipartFile mfile = (CommonsMultipartFile) multipartRequest.getFile("fileToUploadId");

		if (null == fileToUploadId || "".equals(fileToUploadId)) {
			fileToUploadId = "fileToUpload";
		}
	
		String realFileName = "";
		String fileDir = multipartRequest.getParameter("fileDir");

		fileDir = fileDir == null ? "temp" : fileDir;
		
		HttpHeaders responseHeaders = new HttpHeaders();
		
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Map<String, String> stateMap = new HashMap<String, String>();
		try {
			MultipartFile file = multipartRequest.getFile(fileToUploadId);	//根据前台的name名称得到上传的文件
			realFileName = file.getOriginalFilename().trim();	//相同名字
			realFileName = realFileName.replace(" ", "");	//去除空格
			String ext = realFileName.trim().substring(realFileName.lastIndexOf("."));
			String fn = realFileName.trim().substring(0, realFileName.lastIndexOf("."));
			if (file == null || file.getSize() == 0) {
				stateMap.put("STATE", "empty");
				return new ResponseEntity<String>(new Gson().toJson(stateMap), responseHeaders, HttpStatus.CREATED);
			}
			
			realFileName = fn + "_" + sdf.format(new Date()) + ext;
			//取得服务器真实路径
			String ctxPath = multipartRequest.getSession().getServletContext().getRealPath("/") + "\\uploadFile\\" + fileDir + "\\";	// 获取全路径  
			//创建文件
			File dirPath = new File(ctxPath);
			if (!dirPath.exists()) {
				dirPath.mkdir();
			}
			
			File uploadFile = new File(ctxPath + realFileName);
			try {
				FileCopyUtils.copy(file.getBytes(), uploadFile);
			} catch (IOException e) {
				e.printStackTrace();
			}
			String filePath = "//uploadFile//" + fileDir + "//" + realFileName;
			stateMap.put("FILEPATH", filePath);
			stateMap.put("STATE", "success");
			return new ResponseEntity<String>(new Gson().toJson(stateMap), responseHeaders, HttpStatus.CREATED);
		} catch (Exception e) {
			e.printStackTrace();
			stateMap.put("STATE", "error"); 
			return new ResponseEntity<String>(new Gson().toJson(stateMap), responseHeaders, HttpStatus.CREATED);
		}
	}
	
	public String getExtName(String fileDir) {
		 if (null == fileDir || "".equals(fileDir)) {
			 return "";
		 }
		 String prefix = fileDir.substring(fileDir.lastIndexOf(".") + 1);
		 return prefix;
	}
	
}
